<template>
    <el-row class="lives-news">
        <el-row>
            <el-col class="title">
                <h3>观看时段统计</h3>
            </el-col>
        </el-row>
        <el-row class="search-row">
            <el-form class="inline-form" :inline="true" @submit.native.prevent="getUserTime">
                <el-form-item label="搜索">
                    <el-input class="search-input" placeholder="请输入账号" icon="close" v-model="usernameLike" :on-icon-click="emptySearch">
                    </el-input>
                    <el-input class="search-input" placeholder="请输入客户昵称" icon="close" v-model="nameLike" :on-icon-click="emptySearch">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="liveTimeId" placeholder="请选择时间段">
                        <el-option
                          v-for="item in liveTimeList"
                          :key="item.liveTimeId"
                          :label="item.name"
                          :value="item.liveTimeId">
                        </el-option>
                      </el-select>
                </el-form-item>
                <el-form-item>
                    <el-date-picker
                      v-model="date"
                      type="date"
                      placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getUserTime">搜索</el-button><el-button type="danger" @click="emptySearch">清空</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-col>
                <el-table :data="userTimeList">
                    <el-table-column prop="userId" label="客户ID" width="180px" align="center"></el-table-column>
                    <el-table-column prop="username" label="账号" width="140px" align="center"></el-table-column>
                    <el-table-column label="昵称" width="180px" align="center">
                        <template slot-scope="scope">{{scope.row.name?scope.row.name:'-'}}</template>
                    </el-table-column>
                    <el-table-column prop="liveTimeName" label="时段名称" align="center"></el-table-column>
                    <el-table-column label="日期" align="center">
                        <template slot-scope="scope">{{moment(scope.row.date).format('YYYY-MM-DD')}}</template>
                    </el-table-column>
                    <el-table-column label="观看时长" align="center">
                        <template slot-scope="scope">{{scope.row.minutes}}分钟</template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="counts">
                </el-pagination>
            </el-col>
        </el-row>
    </el-row>
</template>
<script>
import {
    getUserTime,
    getLiveTime
} from '@/api/api'
export default {
    data: function() {
        return {
            pageId: 1,
            pageSize: 10,
            counts: 0,
            userTimeList: [],
            nameLike: '',
            usernameLike: '',
            liveTimeId: null,
            liveTimeList: [],
            date: ''
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getUserTime();
        getLiveTime().then(res => {
            this.liveTimeList = res;
        })
    },
    methods: {
        //获取视频分类列表
        getUserTime: function() {
            var date = '';
            if(this.date){
                date = this.moment(this.date).format('YYYY-MM-DD')
            }
            var params = {
                pageSize: this.pageSize,
                pageId: this.pageId,
                name: this.nameLike,
                username: this.usernameLike,
                liveTimeId: this.liveTimeId,
                date: date
            }
            getUserTime({params: params}).then(data => {
                console.log(data)
                var str = '?';
                for (var key in params) {
                    if (params[key]) {
                        str += key + '=' + params[key] + '&'
                    }
                }
                this.$router.push(str);
                this.counts = data.count;
                this.userTimeList = data.list;
            })
        },
        //搜索
        searchArticleByName: function() {
            this.getUserTime();
        },
        //分页
        currentChange: function(val) {
            this.$router.push('?page=' + val)
            this.pageId = val;
            this.getUserTime()
        },
        emptySearch: function(){
            this.nameLike = ''
            this.usernameLike = ''
            this.liveTimeId = null
            this.date = ''
            this.getUserTime()
        }
    }
}
</script>
<style scoped lang="scss">
.lives-news {
    .title {
        font-size: 12px;
        border-bottom: 1px solid #eaeaea;
        margin-bottom: 5px;
        h3 {
            display: inline-block;
            padding: 5px 0;
            margin: 0;
            font-weight: normal;
            color: #23b7e5;
            border-bottom: 1px solid #23b7e5;
        }
    }
}
.search-input{
	width: 200px;
}
</style>